/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import http from '@ohos.net.http';
import prompt from '@system.prompt';
import router from '@system.router';

export default {
    data: {
        name: '',
        phone:'',
        date: '1990-01-01',
        birthDate: '',
        nowDate: '',
        password:"",
        password2:"",
        gender: 'Strings.male',
        identity: 'Strings.identity',
        sex : 1,
        age : '',
        isParent: 0,
        isTeacher: 0,
    },
    onInit() {
    },

    fetch(){
        let httpRequest = http.createHttp();
        // 用于订阅http响应头，此接口会比request请求先返回。可以根据业务需要订阅此消息
        httpRequest.on('headerReceive', (err, data) => {

        });
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
            "http://8.142.187.137:8888/user/register",
            {
                method: 'POST', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json'
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {'phone': this.phone, 'password': this.password, 'username': this.name, 'sex': this.sex, 'age': this.age, 'isParent': this.isParent, 'isTeacher': this.isTeacher},
                readTimeout: 60000, // 可选，默认为60000ms
                connectTimeout: 60000 // 可选，默认为60000ms
            },(err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                var result = JSON.parse(data.result);
                console.info('Result:' + data.result);
                console.info('code:' + data.responseCode);
                // data.header为http响应头，可根据业务需要进行解析
                console.info('header:' + data.header);
                if(result["code"] == '200'){
                    prompt.showToast({
                        message: result.msg
                    })
                    router.push({
                            uri: 'pages/success/success'
                        })


                }else if(result["code"] == '400'){
                    prompt.showToast({
                        message: result.msg
                    })
                }

            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },

    getName(e) {
        this.name = e.value;
        console.info("name=" + this.name)
    },
    getDate(e) {
        this.date = e.year + '-' + (e.month + 1) + '-' + e.day;
        this.birthDate = e.year;
        this.nowDate =new Date().getFullYear();
        this.age = parseInt(this.nowDate) - parseInt(this.birthDate);
        console.info("age="+this.age);
        console.info("date=" + this.date)
    },
    getPhone(e){
        this.phone = e.value;
        console.info("phone="+ this.phone);
    },

    getPassword(e){
        this.password = e.value;
        console.info("password="+this.password);
    },
    getPassword2(e){
        this.password2 = e.value;
        console.info("password2="+this.password2);
    },
    getFemaleGender(e) {
        if (e.checked) {
            this.gender = 'Strings.female';
            this.sex = 0;
            console.info("gender =" + this.gender)
        }
    },
    getMaleGender(e) {
        if (e.checked) {
            this.gender = 'Strings.male';
            this.sex = 1;
            console.info("gender =" + this.gender)
        }
    },
    getIdentity(e) {

        this.identity = e.newValue;
        if(this.identity == '家长') {
            this.is_parent = 1;
        }else if(this.identity == '教师'){
            this.is_teacher = 1;
        }
        console.info("identity=" + this.identity);
    },
    onRegiste() {
        if (this.name.length == 0) {
            prompt.showToast({
                message: this.$t('Strings.name_null')
            })
            return;
        }
        if (this.name.length < 6) {
            prompt.showToast({
                message: this.$t('Strings.name_short')
            })
            return;
        }
        if(this.phone ===""){
            prompt.showToast({
                message: "手机号不能为空"
            })
            return;
        }else if(this.phone.length != 11){
            prompt.showToast({
                message: "手机号输入错误！"
            })
            return;
        }
        if (this.password === ""){
            prompt.showToast({
                message:"密码不能为空"
            })
            return;
        }else if(this.password !== this.password2){
            prompt.showToast({
                message:"两次密码输入不一致"
            })
            return;
        }

        if (this.date.length == 0) {
            prompt.showToast({
                message: this.$t('Strings.date_null')
            })
            return;
        }

        if (this.gender.length == 0) {
            prompt.showToast({
                message: this.$t('Strings.gender_null')
            })
            return;
        }

        if (this.identity.length == 0) {
            prompt.showToast({
                message: this.$t('Strings.identity_null')
            })
            return;
        }
        this.fetch();
    }
}
